package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created by renatagrfarias on 9/18/14.
 */
public class BooksRepository {

    private ArrayList<Book> library = new ArrayList();

    public void createLibrary(){
        createLibrary("0", "Test-Driven Development", "Kent Beck", "2004");
        createLibrary("1", "1984", "George Orwell", "1950");
        createLibrary("2", "The Diary of Anne Frank", "Anne Frank", "2010");
        createLibrary("3", "Where the Wild Things Are", "Maurice Sendak", "2012");
    }

    public void createLibrary(String id, String bookTitle, String author, String year) {
        Book book = new Book(id, bookTitle, author, year);
        library.add(book);
    }

    public void listBooks(){
        System.out.println("---------------- List of books --------------");
        for(int i=0;i<library.size();i++){
            if(!library.get(i).getCheckOut())
                System.out.println(library.get(i).getId() +" "+ library.get(i).getTitle() + " - "+library.get(i).getAuthor() + ", " + library.get(i).getYear());
        }
        System.out.println("---------------------------------------------");
    }

    public Book searchBook(String id){
        Book book = null;
        for(int i=0;i<library.size();i++){
            if(library.get(i).getId().equals(id)){
                book = library.get(i);
            }
        }
        return book;
    }

    public void checkOutBook(Book book, User user){
        book.setCheckOut(true);
        book.setUser(user);
    }

    public void returnBook(Book book, User user){
        book.setCheckOut(false);
        book.setUser(user);
    }

    public boolean bookAvailability(Book book){
        if(book !=null) {
            if (book.getCheckOut()) {
                return false;
            }
            return true;
        }else
            return false;
    }

}

